<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>向生成器中抛入一个异常</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="generator.send.html">« Generator::send</a></li>
      <li style="float: right;"><a href="generator.valid.html">Generator::valid »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.generator.html">Generator</a></li>
    <li>向生成器中抛入一个异常</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="generator.throw" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Generator::throw</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">Generator::throw</span> &mdash; <span class="dc-title">向生成器中抛入一个异常</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-generator.throw-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Generator::throw</strong></span>(<span class="methodparam"><span class="type"><a href="class.throwable.html" class="type Throwable">Throwable</a></span> <code class="parameter">$exception</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   抛出一个异常到生成器并恢复生成器的执行。
   与当前 <a href="language.generators.syntax.html#control-structures.yield" class="link">yield</a> 表达式被 <code class="literal">throw $exception</code> 语句替换是一样的行为。
  </p>
  <p class="para">
   如果调用此方法时生成器已经关闭，则将会在调用者的上下文中抛出异常。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-generator.throw-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">exception</code></dt>

    <dd>

     <p class="para">
      抛出异常到生成器。
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-generator.throw-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回生成的值。
  </p>
 </div>


  <div class="refsect1 examples" id="refsect1-generator.throw-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-402">
    <p><strong>示例 #1 抛出异常到生成器</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">gen</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Foo\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Exception:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">()}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Bar\n"</span><span style="color: #007700">;<br />}<br />&nbsp;<br /></span><span style="color: #0000BB">$gen&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gen</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$gen</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$gen</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">throw</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">Exception</span><span style="color: #007700">(</span><span style="color: #DD0000">'Test'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Foo
Exception: Test
Bar
</pre></div>
    </div>
   </div>
  </p>
 </div>

</div></div></div></body></html>